#!/usr/bin/env python
#
# Copy this in ~/.gimp-2.x/plug-ins/
# and restart gimp

import math
import os
import sys
import glob
from gimpfu import *

def MultiText(folder, height, width, fontname, fontsize):
    """
    Params:
      folder: where to find the files
      fontname: font to use
      fontsize: size of the font to use.
    """
    img = gimp.Image(height, width, RGB)
    img.disable_undo()
    layer = gimp.Layer(img, "bottom-layer", 
        width, height, RGB_IMAGE, 100, NORMAL_MODE)
    img.add_layer(layer, 0)
    pdb.gimp_edit_fill(layer, BACKGROUND_FILL)
 
    print "About to glob", folder
    for index, fname in enumerate(glob.glob(folder)):
      print fname
      txt = file(fname).read()
      OneLayer(img, width, height, fontname, fontsize, txt, index, width / 40, height / 20)

    img.enable_undo()
    gimp.Display(img)

def SaveImage(fname, img):
    pdb.gimp_image_convert_indexed(
        img,
        2, # 0 is no dither, 2 FSlow bleed dither
        0, # 0 is Make palette, 2 Web palette
        128, # with 255 colors
        1, # Don't dither transparency
        1, # Remove unused
        '') # name of new palette.
    pdb.gimp_image_set_filename(img, fname)
    #pdb.gimp_file_save(
    #    img,
    #    img.layers[0],
    #    fname,
    #    fname)
    pdb.gimp_image_clean_all(img)
    pdb.file_gif_save(
        img,
        img.layers[0],
        fname,
        fname,
        0, # not interlaced
        1, # loop once
        120, # Default delay
        1) # combine (save space)


    print "Saved to '%s'" % fname

def OneLayer(img, width, height, fontname, fontsize, txt, index, x, y):
    """
    Create one layer and draw the text and a certain y position, centered on
    the width of the image.

    Params:
      img: image to draw on
      width: width of image in pixels
      height: height of image in pixels
      fontname: name of font used
      txt: text to draw (ex. "0\n1")
      y: Y position to draw the text, may be negative
    """
    (txt_w, txt_h, ascent, descent) = pdb.gimp_text_get_extents_fontname( 
        txt, fontsize, PIXELS, fontname) 
    txtfloat = pdb.gimp_text_fontname(img, img.active_layer, 
        x, y, 
        txt, -1, # Border
        True, # AntiAliased
        fontsize, PIXELS, fontname) 

    # Merge with background
    #pdb.gimp_floating_sel_anchor(txtfloat)

    return 

register(
        "python_multitext",
        "Import a bunch of text, one over the other",
        "Import a bunch of text one over the other",
        "Scott Kirkwood",
        "Scott Kirkwood",
        "2007",
        "<Toolbox>/Xtns/Python-Fu/Multi-Text...",
        "",
        [
                (PF_STRING, "folder", "Folder", "/home/scott/www/tech-talk/pythonvsbash/sh/*.sh"),
                (PF_INT, "height", "Height", 3508),
                (PF_INT, "width", "Width", 2480),
                (PF_STRING, "fontname", "Font Name", "Nimbus Mono L"),
                (PF_INT, "fontsize", "Font Size", 32),
        ],
        [],
        MultiText
        )

main()
